﻿/// <reference path="jquery.js" />
/// <reference path="jquery-ui.js" />
/// <reference path="svg.js" />
/// <reference path="pv.core.js" />
/// <reference path="pv.node.js" />

function villageHP(hpDiff) {
    village.hp += hpDiff;

    $("#hp").progressbar({
        value: village.hp / village.max_hp * 100
    });

    if (village.hp < 0) {
        gameOver(false);
    }
}

function getLevel() {
    //query = query.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var expr = "[\\?&]l=([^&#]*)";
    var regex = new RegExp(expr);
    var results = regex.exec(window.location.href);
    if (results !== null) {
        return results[1];
        //return decodeURIComponent(results[1].replace(/\+/g, " "));
    } else {
        return 1;
    }
}

function gameBegin() {
    var level = getLevel();
    $("#dialog").attr("title", "第" + level + "关");
    $("#dialog").text(db.levels[level - 1].des);
    $("#dialog").dialog({
        modal: true,
        buttons: {
            Ok: function () {
                $(this).dialog("close");
                db.levels[level - 1].scenario();
            }
        }
    });
}

function gameOver(isWin) {
    pv.pause = true;
    if (isWin) {
        $("#dialog").text("Next");
        $("#dialog").dialog({
            modal: true,
            buttons: {
                Ok: function () {
                    window.location.href = "?l=" + (getLevel() + 1);
                }
            }
            //close: function () { location.reload(); }
        });
    }
    else {
        $("#dialog").text("Game Over");
        $("#dialog").dialog({
            modal: true,
            buttons: {
                Ok: function () {
                    location.reload();
                }
            }
        });
    }
}
var village = {
    max_hp: 500,
    hp: 500,
    armor: 12,
    max_ch: 2,
    max_stone: 3
};

var db = {
    levels: [{
        des: "捡石头，砸土匪呀！",
        weapons: [],
        scenario: function () {
            var ch = 0;
            var stone = 0;

            var opt = new PvProjectileOpt();
            opt.disposed = function (sender, e) {
                ch--;
            }
            pv.setInterval(function () {
                if (ch < village.max_ch) {
                    ch++;
                    new PvChainProjectile("ch", { x: 1200, y: Math.random() * 500 + 100 }, opt);
                }
            }, 3);

            pv.setInterval(function () {
                if (stone < village.max_stone) {
                    stone++;
                    new PvProjectile("stone", { x: 1250, y: Math.random() * 600 + 100 }, {
                        disposed: function () {
                            stone--;
                        }
                    });
                }
            }, 1);
            var killedBandits = 0;
            function createBandit() {
                new PvTarget("bandit", { x: 0, y: Math.random() * 600 + 100 }, {
                    attack: function (sender, e) {
                        var realDemage = sender.status.demage - village.armor;
                        if (realDemage > 0) {
                            villageHP(-realDemage);
                        }
                    },
                    killed: function (sender, e) {
                        killedBandits++;
                        if (killedBandits == 3)
                            gameOver(true);
                    }
                });
            }

            pv.setInterval(createBandit, 7, { times: 3 });
        }
    }, {
        des: "武器店开张了！ 双击买个滚木玩玩",
        weapons: [],
        scenario: function () {
        }
    }]

};